package com.zxy.supplier_system.server.utils

import io.jsonwebtoken.Claims
import io.jsonwebtoken.Jwts
import io.jsonwebtoken.SignatureAlgorithm
import java.util.*
import javax.crypto.spec.SecretKeySpec


fun parseToClaims(token: String, secret: String): Claims {
    return Jwts.parserBuilder().setSigningKey(secret.toByteArray()).build().parseClaimsJws(token).body
}

fun generateToken(
    subject: String, secret: String,
    claims: Map<String, Any> = emptyMap(), issuedAt: Date = Date(),
    expiredAt: Date? = null
): String {
    val key = SecretKeySpec(secret.toByteArray(),SignatureAlgorithm.HS256.jcaName)
    return Jwts.builder()
        .setSubject(subject)
        .setIssuedAt(issuedAt)
        .setExpiration(expiredAt)
        .addClaims(claims)
        .signWith(key)
        .compact()
}

